package com.forekenow.Demo6.Exercise;


public class EmployeeDAOImpl implements EmployeeDao {
    private Employee[] employees;
    static int count;
    public EmployeeDAOImpl() {
        //利用构造器初始化数组
        employees = new Employee[3];
    }

    @Override
    public void add(Employee employee) {
        //1.判断传进来的employee参数 是否为空
        //2.如果count<3
        //3.将emoloyee添加数组容器中，count+1
        //4.输出信息
        if (employees != null&&count<3){
            employees[count] = employee;
            count++;
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    @Override
    public Employee[] select() {
        //1.要对employee数组进行遍历，每遍历一次要将原数组中获取的员工对象保存到新的数组当中
        //2.返回新的数组
        Employee[] employees = new Employee[3];
            for (int i = 0; i<= 2; i++){
            employees[i] = this.employees[i];
        }
        return employees;
    }

    @Override
    public void modified(Employee employee) {
        //1.可以先根据id查询要修改的对象
        //2.将查询到的对象传递给参数进行修改
        for (int i = 0; i < 3; i++) {
            employees[i] = this.employees[i];
        }
            if (employee.getId()==3){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }

    }

    @Override
    public Employee[] find(int id) {
        //1.对employee数组进行遍历
        //2.每遍历一次就要获取到一个Employee对象·判断Employee对象id是否与参数中的id是否相等
        //3.最后将查询到的对象返回
        Employee[] employees = new Employee[3];
        for (int i = 0; i< 3; i++){
            employees[i] = this.employees[i];
            System.out.println();
        }
        return employees;
    }

    public static void main(String[] args) {
        //接口                              实现类
        EmployeeDao employeeDAO = new EmployeeDAOImpl();
        Employee employee = new Employee(3, "李四", 30000, "财务部");
        employeeDAO.add(employee);
        Employee[] employees1 = employeeDAO.select();
        for (int i = 0; i < count; i++) {
            System.out.println(employees1[i].getId() + employees1[i].getName() + employees1[i].getSal() + employees1[i].getDept());
        }
        employeeDAO.modified(employee);
        Employee[] ss = employeeDAO.find(3);
            for (int i = 0; i < count; i++) {
                if (employee.getId()==2){
                    System.out.println("查找成功");
                employee = ss[i];
                System.out.println(employee.getName() + employee.getId() + employee.getSal() + employee.getDept());
            }else {
                    System.out.println("查找失败");
                }
        }
    }
}
